package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.errorprone.annotations.Var;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

	@Autowired
	private SystemUserMapper systemUserMapper;
	@Autowired
	private StringRedisTemplate redisTemplate;

	// 验证码生成
	@Override
	public CaptchaVo getCaptcha() {
		SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
		specCaptcha.setCharType(Captcha.TYPE_DEFAULT);
		// 获取验证码中的 文本内容
		String code = specCaptcha.text().toLowerCase();
		// 图片流 base64编码
		String image =specCaptcha.toBase64();
		// 生成key
		String key = RedisConstant.ADMIN_LOGIN_PREFIX+UUID.randomUUID();
		// 存入redis并设置过期时间为30分钟
		redisTemplate.opsForValue().set(key, code,RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);

		// 返回的CaptchaVo类
		CaptchaVo captchaVo = new CaptchaVo(image,key);


		return captchaVo;
	}

	// 登录成功并创建token
	@Override
	public String login(LoginVo loginVo) {
		String username = loginVo.getUsername();
		String password = loginVo.getPassword();
		String captchaKey = loginVo.getCaptchaKey();
		String captchaCode = loginVo.getCaptchaCode();

		// 验证码校验阶段
		// 1. 先判断是否输入了验证码
		if(!StringUtils.hasText(captchaCode)){
			throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND);
		}

		// 2. 是判断验证码是否过期
		String code = redisTemplate.opsForValue().get(captchaKey);
		if(code == null){
			throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
		}

		// 2.2 判断验证码是否正确
		if(!code.equals(captchaCode.toLowerCase())){
			throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
		}


		// 账号密码校验阶段
		// 先校验用户是否存在
		SystemUser systemUser = systemUserMapper.selectOne(new LambdaQueryWrapper<SystemUser>().eq(SystemUser::getUsername, username));
		if(systemUser == null){
			throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
		}

		// 再校验用户是否被禁用
		if(systemUser.getStatus() == BaseStatus.DISABLE){
			throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
		}

		// 校验用户密码是否正确
		if(!systemUser.getPassword().equals(DigestUtils.md5Hex(password))){
			System.out.println(DigestUtils.md5Hex(password));
			throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
		}

		String token = JwtUtil.creatToken(systemUser.getId(), systemUser.getUsername());
		return token;
	}
}